/**
 * Copyright (C) 2013 DaiKit.com - daikit-commons module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.commons.shared.exception;

import com.daikit.commons.shared.exception.context.DkExceptionContext;



/**
 * Exception that all DK exceptions should extend.
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class DkException extends RuntimeException
{

	private static final long serialVersionUID = 4114937949535618292L;

	private DkExceptionContext context;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CONSTRUCTORS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Default Constructor. This one should not be used but is here for serialization purpose.
	 */
	@Deprecated
	public DkException()
	{
		super();
	}

	/**
	 * Constructs a new DK exception with given message. Create a Context that can be accessed by {@link #getContext()}.
	 *
	 * @param code
	 *           the exception code
	 * @param message
	 *           the message for the exception
	 */
	public DkException(final String code, final String message)
	{
		super(message);
		context = new DkExceptionContext(code, getTypeCode(), message);
	}

	/**
	 * Constructs a new DK exception with given message. Create a Context that can be accessed by {@link #getContext()}.
	 *
	 * @param code
	 *           the exception code
	 * @param message
	 *           the exception message
	 * @param cause
	 *           the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value
	 *           is permitted, and indicates that the cause is nonexistent or unknown.)
	 */
	public DkException(final String code, final String message, final Throwable cause)
	{
		super(message, cause);
		context = new DkExceptionContext(code, getTypeCode(), message);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// ABSTRACT METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return the exception type code.
	 */
	public String getTypeCode()
	{
		final String simpleName = getClass().getName().substring(getClass().getName().lastIndexOf(".") + 1);
		final int index = simpleName.lastIndexOf("Exception");
		return index == -1 ? simpleName : simpleName.substring(0, index);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return the context
	 */
	public DkExceptionContext getContext()
	{
		return context;
	}

	/**
	 * @param context
	 *           the context to set
	 */
	public void setContext(final DkExceptionContext context)
	{
		this.context = context;
	}

}
